//
//  YCLeagueInfo.m
//  LOLBox
//
//  Created by bb on 15-3-29.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCLeagueInfo.h"
#import "YCInfoModel.h"
#import "YCInfoCell.h"
#import "YCHeader.h"
#import "YCPowerViewController.h"
#import "YCAppDelegate.h"
#import "YCZBViewController.h"

@interface YCLeagueInfo ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *headerArr;

@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation YCLeagueInfo

- (void)viewWillAppear:(BOOL)animated
{
    YCAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate showTabBar];
    animated = YES;

}

- (id)init
{
    self = [super init];
    if (self) {
        self.dataSource = [NSMutableArray array];
        self.headerArr = [NSMutableArray array];
        
        //data
        [self initAllData];
    }
    return self;
}
- (void)initAllData
{
    //header
    [_headerArr addObject:@"工具"];
    [_headerArr addObject:@"周边"];
    [_headerArr addObject:@"其他"];
    
    //first
    NSArray *nameArr1 = @[@"战斗力",@"英雄对英雄",@"物品",@"符文模拟器",@"天赋模拟器",@"召唤师技能",@"圈子",@"账号查询工具"];
    NSArray *imgArr1 = @[@"icon_card",@"icon_vs",@"icon_wupin",@"icon_fuwen",@"icon_s4",@"more_sum_ability",@"icon_quanzi",@"icon_pingheng"];
    
    //second
    NSArray *nameArr2 = @[@"联盟信息",@"攻略心得",@"比赛直播",@"赛事日历",@"限时折扣",@"周边配音",@"排行榜",@"国服英雄榜",@"比赛竞猜"];
    NSArray *imgArr2 = @[@"icon_picture",@"icon_gonglue",@"icon_live",@"icon_bisai",@"icon_sale",@"icon_sound",@"icon_paihang",@"icon_weekdata",@"icon_shen"];
    
    //third
    NSArray *nameArr3 = @[@"超级辅助系列",@"超级辅助小店",@"签到"];
    NSArray *imgArr3 = @[@"icon_quanzi",@"icon_taobao_sale",@"icon_signin"];
    
    NSMutableArray *arr1 = [NSMutableArray array];
    NSMutableArray *arr2 = [NSMutableArray array];
    NSMutableArray *arr3 = [NSMutableArray array];
    
    for (NSInteger i = 0; i < nameArr1.count; i++)
    {
        YCInfoModel *model = [[YCInfoModel alloc] init];
        model.name = nameArr1[i];
        model.img = imgArr1[i];
        [arr1 addObject:model];
    }
    
    for (NSInteger i = 0; i < nameArr2.count; i++)
    {
        YCInfoModel *model = [[YCInfoModel alloc] init];
        model.name = nameArr2[i];
        model.img = imgArr2[i];
        [arr2 addObject:model];
    }
    
    for (NSInteger i = 0; i < nameArr3.count; i++)
    {
        YCInfoModel *model = [[YCInfoModel alloc] init];
        model.name = nameArr3[i];
        model.img = imgArr3[i];
        [arr3 addObject:model];
    }
    
    [_dataSource addObject:arr1];
    [_dataSource addObject:arr2];
    [_dataSource addObject:arr3];
    
    [_collectionView reloadData];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        [self createNavTitleView:@"联盟资料"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createCollectionView];
    
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(64, 84);
    layout.sectionInset = UIEdgeInsetsMake(30, 0, 30, 0);
    layout.headerReferenceSize = CGSizeMake(320, 20);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight([[UIScreen mainScreen] bounds]) - 50 - 64) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"YCInfoCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"YCHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataSource.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *arr = _dataSource[section];
    return arr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YCInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    NSArray *arr = _dataSource[indexPath.section];
    YCInfoModel *model = arr[indexPath.row];
    
    [cell fillCellWithModel:model];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击");
    //战斗力
    if (indexPath.section == 0 && indexPath.row == 0)
    {
        NSLog(@"战斗力查询");
        YCPowerViewController *power = [[YCPowerViewController alloc] init];
        [self.navigationController pushViewController:power animated:YES];
    }
    //装备
    if (indexPath.section == 0 && indexPath.row == 2)
    {
        YCZBViewController *zb = [[YCZBViewController alloc] init];
        [self.navigationController pushViewController:zb animated:YES];
    }
    if (indexPath.section == 0 && indexPath.row == 1)
    {
        UIViewController *vc = [[UIViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(64, 84);
}
//header And Footer
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    YCHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    
    NSString *title = _headerArr[indexPath.section];
    
    [headerView initTitle:title];
    return headerView;
 
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{

    CGFloat lengFloat = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    CGFloat spLeng = (lengFloat - 64 * 3) / 5;
    return  UIEdgeInsetsMake(5, spLeng, 5, spLeng);

}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
